package com.atguigu.yygh.hosp.controller.Api;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;


import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.data.domain.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    HospitalService hospitalService;
    @Autowired
    HospitalSetService hospitalSetService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    ScheduleService scheduleService;
//    上传医院接口
    @PostMapping("saveHospital")
    @ApiOperation(value = "上传医院接口")
    public Result  saveHosp(HttpServletRequest request) throws Exception {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parmmap = HttpRequestHelper.switchMap(requestMap);
//        1.获取医院系统传递过来的签名
       String hospsign = (String) parmmap.get("sign");
        System.out.println(hospsign);
        String hoscode = (String) parmmap.get("hoscode");

         String signKey=    hospitalSetService.getSignKey(hoscode);

        String md5sign = MD5.encrypt(signKey);
        System.out.println(md5sign);
        if(!hospsign.equals(md5sign))
        {
            throw new Exception();
        }
//        因为图片是base64加密+号变空格
        String logoData = (String)parmmap.get("logoData");
        logoData = logoData.replaceAll(" " ,"+");
        parmmap.put("logoData",logoData);
        hospitalService.save(parmmap);
        return Result.ok();
    }
//    上传科室接口
    @PostMapping("saveDepartment")
    public  Result saveDepartment(HttpServletRequest request) throws Exception {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parmmap = HttpRequestHelper.switchMap(requestMap);

        String hospsign = (String) parmmap.get("sign");
        System.out.println(hospsign);
        String hoscode = (String) parmmap.get("hoscode");

        String signKey=    hospitalSetService.getSignKey(hoscode);

        String md5sign = MD5.encrypt(signKey);
        System.out.println(md5sign);
        if(!hospsign.equals(md5sign))
        {
            throw new Exception();
        }

        departmentService.save(parmmap);
        return Result.ok();
    }
//    上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request)
    {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parmmap = HttpRequestHelper.switchMap(requestMap);
        scheduleService.save(parmmap);
        return  Result.ok();
    }

//    查询医院的接口
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) throws Exception {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> parmmap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) parmmap.get("hoscode");
        String hospsign = (String) parmmap.get("sign");
        System.out.println(hospsign);


        String signKey=    hospitalSetService.getSignKey(hoscode);

        String md5sign = MD5.encrypt(signKey);
        System.out.println(md5sign);
        if(!hospsign.equals(md5sign))
        {
            throw new Exception();
        }
          Hospital hospital= hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

//    查询科室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) throws Exception {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> parmMap = HttpRequestHelper.switchMap(parameterMap);

        int page=  StringUtils.isEmpty(parmMap.get("page"))?1:Integer.parseInt((String)parmMap.get("page"));
        int limit=  StringUtils.isEmpty(parmMap.get("limit"))?1:Integer.parseInt((String)parmMap.get("limit") );
        String hoscode = (String) parmMap.get("hoscode");


        String hospsign = (String)parmMap.get("sign");

        String signKey=    hospitalSetService.getSignKey(hoscode);

        String md5sign = MD5.encrypt(signKey);

        if(!hospsign.equals(md5sign))
        {
            throw new Exception();
        }
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
         Page<Department> departmentPage= departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(departmentPage);

    }
//    查询排班接口
    @PostMapping("schedule/list")
    public  Result findSchedule(HttpServletRequest request)
    {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> parmMap = HttpRequestHelper.switchMap(parameterMap);
        int page=  StringUtils.isEmpty(parmMap.get("page"))?1:Integer.parseInt((String)parmMap.get("page"));
        int limit=  StringUtils.isEmpty(parmMap.get("limit"))?1:Integer.parseInt((String)parmMap.get("limit") );
//        医院编号
        String hoscode = (String) parmMap.get("hoscode");
//        科室编号
        String depcode=(String) parmMap.get("depcode");

        ScheduleQueryVo queryVo = new ScheduleQueryVo();
        queryVo.setDepcode(depcode);
        queryVo.setHoscode(hoscode);
        Page<Schedule> schedulePage= scheduleService.findPageSchedule(page,limit,queryVo);
        return Result.ok(schedulePage);
    }
//    删除科室接口
    @PostMapping("department/remove")
    public Result departmentRemove(HttpServletRequest request) throws Exception {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> parmMap = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) parmMap.get("hoscode");
        String depcode= (String) parmMap.get("depcode");
        String hospsign = (String)parmMap.get("sign");

        String signKey=    hospitalSetService.getSignKey(hoscode);

        String md5sign = MD5.encrypt(signKey);

        if(!hospsign.equals(md5sign))
        {
            throw new Exception();
        }
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }
    @PostMapping("schedule/remove")
    public  Result removeSchedule(HttpServletRequest request)
    {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> parmMap = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) parmMap.get("hoscode");
        String hosScheduleId = (String) parmMap.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

}
